package com.aigis.big4.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;

public class HdfsOperateUtil {
    /**
     * 获取hdfs配置
     * @return hdfs 文件系统
     */
    public static FileSystem getFileSystem() throws IOException {
        Configuration conf=new Configuration();
        conf.set("fs.defaultFS","hdfs://localhost:9000/");
        FileSystem fileSystem= FileSystem.get(conf);
        return fileSystem;
    }

    /**
     *打开文件
     * @param pathStr 文件路径
     */
    public static void openFile(String pathStr) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path path=new Path(pathStr);
        FSDataInputStream fis=fileSystem.open(path);
        byte[] bytes=new byte[200];
        int i=fis.read(bytes);
        System.out.println(new String(bytes,0,i));
        fis.close();
        fileSystem.close();
    }

    /**
     * 创建并写入文件
     * @param pathStr
     * @throws IOException
     */
    public static void createFile(String pathStr) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path path=new Path(pathStr);
        FSDataOutputStream fos=fileSystem.create(path);
        fos.writeChars("write test");
        fos.close();
        fileSystem.close();
    }

    /**
     * 追加写入
     * @param pathStr
     */
    public static void appendFile(String pathStr) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path path=new Path(pathStr);
        FSDataOutputStream fos=fileSystem.append(path);
        fos.writeChars("append test");
        fos.close();
        fileSystem.close();
    }

    /**
     * 文件重命名
     * @param inName
     * @param outName
     * @throws IOException
     */
    public static void  renameFile(String inName,String outName) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path inPath=new Path(inName);
        Path outPath=new Path(outName);
        boolean flag=fileSystem.rename(inPath,outPath);
        System.out.println(flag);
    }

    /**
     * 列出文件
     * @param pathStr
     */
    public static void listFile(String pathStr) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path path=new Path(pathStr);
        RemoteIterator<LocatedFileStatus> list=fileSystem.listFiles(path,true);
        while (list.hasNext()){
            System.out.println(list.next());
        }
    }

    /**
     * 创建文件夹
     * @param pathStr 文件夹路径
     * @return 是否创建
     * @throws IOException
     */
    public static boolean createDir(String pathStr) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        Path path=new Path(pathStr);
        boolean flag=fileSystem.mkdirs(path);
        fileSystem.close();
        return  flag;
    }

    /**
     * 上传本地文件到hdfs文件系统
     * @param filePath
     * @param hdfsPath
     * @throws IOException
     */
    public static void uploadFile(String filePath,String hdfsPath) throws IOException {
        FileSystem fileSystem=HdfsOperateUtil.getFileSystem();
        fileSystem.copyFromLocalFile(new Path(filePath),new Path(hdfsPath));
        System.out.println("upload Success");
    }
}
